package com.csot.job;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.StrUtil;
import com.csot.model.ZtIdleIp;
import com.csot.model.ZtIp;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ZtToip {

    Log log = Log.getLog(ZtToip.class);

    private ZtIp dao = new ZtIp().dao();

    private ZtIdleIp idao = new ZtIdleIp().dao();

    private final String[] citys = {"sy",
            "dl",
            "as",
            "tl",
            "bx",
            "dd",
            "jz",
            "fs",
            "yk",
            "fx",
            "ly",
            "pj",
            "cy",
            "hld"};

    public void ztidleip() {
        System.out.println("开始稽核空闲中泰ip");
        Db.tx(() -> {
            Db.delete("delete from t_zt_idle_ip");
//            Db.update("insert into t_zt_idle_ip SELECT * from t_zt_ip a where not EXISTS(select 1 from t_city_ip b where b.ip_address = a.ip) and not EXISTS (select 1 from t_coarse_ip c where c.ip_address = a.ip)");
            for (String s : citys) {
                List<ZtIdleIp> ztIdleIps = idao.find("SELECT * from t_zt_ip a where a.ip_city = ? and not EXISTS(select 1 from t_city_ip b where b.ip_address = a.ip) " +
                        "and not EXISTS (select 1 from t_coarse_ip c where c.ip_address = a.ip)", s);
                Db.batchSave(ztIdleIps, 999);
            }
            return true;
        });
    }

    public void ztip() {
        List<ZtIp> ztIps = new ArrayList<>();
        String filePath = "/opt/app/ipproj/ztdata/upload";
        // HJF_yyyymmdd.txt
        String datestr = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
        String ftpfile = filePath + "/HJF_" + datestr + ".txt";
        try {
            Files.lines(Paths.get(ftpfile)).forEach(line -> {
                // 处理每一行数据
//                System.out.println(line);
                if (StrUtil.isNotBlank(line)) {
                    String[] strs = line.split(" ");
                    ZtIp ztIp = new ZtIp();
                    ztIp.setIp(strs[0]);
                    ztIp.setIpI(NetUtil.ipv4ToLong(ztIp.getIp()));
                    ztIp.setCrIp(strs[1]);
                    ztIp.setCrDescription(strs[2]);
                    String[] citys = strs[2].split("-");
                    ztIp.setIpCity(citys[2].toLowerCase());
                    ztIp.setCreateTime(new Date());
                    ztIp.setUpdateTime(new Date());
                    ztIps.add(ztIp);
                }
            });
            boolean b = Db.tx(() -> {
                try {
                    Db.delete("delete from t_zt_ip");
                    int[] ints = Db.batchSave(ztIps, 999);
                    return true;
                } catch (Exception e1) {
                    e1.printStackTrace();
                    log.error("", e1);
                    return false;
                }
            });
            if (b) {
                log.info("数据保存成功");
                String dates = DateUtil.format(DateUtil.offsetDay(new Date(), -30), DatePattern.PURE_DATE_PATTERN);
                String delfile = filePath + "/HJF_" + dates + ".txt";
                if (FileUtil.exist(delfile)) {
                    FileUtil.del(delfile);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.error("", e);
        }

    }


}
